<?php

namespace App\Repositories\Eloquent\Lst;

use Illuminate\Container\Container as App;
use App\Repositories\Eloquent\Repository;
use Illuminate\Database\Eloquent\Builder;

use App\Repositories\Eloquent\Mgt\BranchRepository as Branch;
use App\Repositories\Eloquent\Edu\TermRepository as Term;

class PublishRepository extends Repository
{
    protected $branch, $term;

    public function __construct(App $app, Branch $branch, Term $term)
    {
        parent::__construct($app);

        $this->branch = $branch;

        $this->term = $term;
    }

    /**
     * 指定模型名称
     *
     * @return mixed
     */
    function model()
    {
        return 'App\Models\Lst\Publish';
    }


    /**
     * loader 延迟预加载参数
     * @param  [type] $loader [description]
     * @return [type]         [description]
     */
    public function index()
    {
        $result = $this->model
                ->with('details', 'term', 'branch', 'type','details.clase','details.category')
                ->where('status',STATUS_NORMAL)
                ->orderBy('id', 'desc')->first();

        return $result;
    }


    public function lists()
    {
        $result = $this->model
                ->with('details', 'term', 'branch', 'type','details.clase','details.category')
                ->orderBy('id', 'desc')->get();

        return $result;
    }

    /**
     * 根据培训类型按分支机构查询活跃的发布方案第一条
     * @param  [type] $tid [description]
     * @return [type]      [description]
     */
    public function achieve($tid)
    {
        //根据type_id查询发布方案中符合要求的学期
        $terms = $this->term->index()->select('id as termid')->where('type_id', $tid);

        $result = $this->model
                ->with('infos', 'term', 'branch','type',
                    'infos.clase', 'infos.clase.category', 'infos.clase.users')
                ->joinSub($terms, 'term', function($join){
                    $join->on('term_id', '=', 'term.termid');
                })
                ->where('status', STATUS_NORMAL)
                ->orderBy('edu_deploy.id', 'desc')
                ->first();

        return $result;

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store($request)
    {//dd($request->clases);
        \DB::beginTransaction();
        try{
            $publish = $this->create([
                'name'      => $request->name,
                'term_id'   => $request->termid,
                'limit'     => $request->limit,
            ]);

            if (!!$publish) {
                $row = array();
                foreach( $request->clases as $clase){
                    $row[] = array('category_id'=>$clase['category'],'clase_id'=>$clase['clase']);
                }
                $publish->details()->createMany($row);
            }

            \DB::commit();

            return $this->responseSuccess('花名册建立成功', $publish, route('student.publish.index'));

        } catch (Exception $e) {

            \DB::rollback();

            return $this->responseFail(500,'花名册建立失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $publish = $this->model
                ->with('details', 'term', 'term.branch','term.type','term.users','term.categories',
                    'term.clases','term.categories.clases','term.categories.clases.users',
                    'details.clase', 'details.clase.category', 'details.clase.users')
                ->find($id);

        if (!$publish) {
            return $this->responseFail(404);
        }

        return $publish;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function renew($request, $id)
    {
        \DB::beginTransaction();
        try{
            $result = $this->update([
                'name'      => $request->name,
                'limit'     => $request->limit,
            ],$id);

            if (!!$result) {
                $publish = $this->model->find($id);
                $publish->details()->delete();

                $row = array();
                foreach( $request->clases as $clase){
                    $row[] = array('category_id'=>$clase['category'],'clase_id'=>$clase['clase']);
                }

                $publish->details()->createMany($row);
            }

            \DB::commit();

            return $this->responseSuccess('花名册修改成功', $publish, route('student.publish.index'));

        } catch (Exception $e) {

            \DB::rollback();

            return $this->responseFail(500,'花名册修改失败');
        }
    }


    public function change($request)
    {
        $result = $this->update([
            'status' => !$request->status,
        ], $request->id);


        if(!!$result)
            return $this->responseSuccess('更改状态成功', null, route('student.publish.index'));

        return $this->responseFail(500,'操作失败');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


}